﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using System.Configuration;
using System.Web;

namespace GenerateMoudlePemission
{
    public partial class DataGenerateTool : Form
    {
        string connstr = ConfigurationManager.ConnectionStrings["TBMDB"].ConnectionString;
        public DataGenerateTool()
        {
            InitializeComponent();
        }

        private void btnBuild_Click(object sender, EventArgs e)
        {
            Type[] tbs = Assembly.Load("TBM.Mvc").GetTypes();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connstr);
            cmd.Connection.Open();
            cmd.CommandText = @"p_UpdateModels";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ControllerName", SqlDbType.VarChar, 30);
            cmd.Parameters.Add("@ControllerDescr", SqlDbType.NVarChar, 10);
            cmd.Parameters.Add("@ActionName", SqlDbType.VarChar, 30);
            cmd.Parameters.Add("@ActionDescr", SqlDbType.NVarChar, 10);
            cmd.Parameters.Add("@ParentAction", SqlDbType.VarChar, 30);

            foreach (Type type in tbs)
            {
                if (type.BaseType != null && (type.BaseType.Name == "Controller" || type.BaseType.Name == "MasterController"))//如果是Controller
                {
                    string strDescr = null;
                    object[] attr = type.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                    object[] p = type.GetCustomAttributes(typeof(System.Web.Mvc.AuthorizeAttribute), true);

                    if (p.Length == 0) continue;

                    if (attr.Length > 0)
                    {
                        strDescr = (attr[0] as System.ComponentModel.DescriptionAttribute).Description;
                    }
                    else
                    {
                        continue;
                    }

                    MethodInfo[] methodes = type.GetMethods();

                    // 添加Modules
                    foreach (MethodInfo method in methodes.OrderBy(a => a.ReturnType.Name))
                    {
                        object[] attrDescr = method.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
                        object[] attrParent = method.GetCustomAttributes(typeof(TBM.Attributes.ParentAction), true);

                        if (attrDescr.Length == 0) continue;

                        if (attrParent.Length == 0)
                        {
                            cmd.Parameters["@ControllerName"].Value = type.Name.Substring(0, type.Name.Length - 10);
                            cmd.Parameters["@ControllerDescr"].Value = strDescr;
                            cmd.Parameters["@ActionName"].Value = method.Name;
                            cmd.Parameters["@ActionDescr"].Value = (attrDescr[0] as System.ComponentModel.DescriptionAttribute).Description;
                            cmd.Parameters["@ParentAction"].Value = DBNull.Value;
                        }
                        else
                        {
                            cmd.Parameters["@ControllerName"].Value = type.Name.Substring(0, type.Name.Length - 10);
                            cmd.Parameters["@ControllerDescr"].Value = strDescr;
                            cmd.Parameters["@ActionName"].Value = method.Name;
                            cmd.Parameters["@ActionDescr"].Value = (attrDescr[0] as System.ComponentModel.DescriptionAttribute).Description;
                            if (attrParent.Length == 0)
                            {
                                cmd.Parameters["@ParentAction"].Value = "";
                            }
                            else
                            {
                                cmd.Parameters["@ParentAction"].Value = (attrParent[0] as TBM.Attributes.ParentAction).Name;
                            }
                        }

                        cmd.ExecuteNonQuery();
                    }
                }
            }

            cmd.Connection.Close();
            MessageBox.Show("完成");
        }

        /// <summary>
        /// 生成菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerateMenu_Click(object sender, EventArgs e)
        {
            TBM.Core.Helper.SiteMenu menu = new TBM.Core.Helper.SiteMenu();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = new SqlConnection(connstr);
            cmd.Connection.Open();
            cmd.CommandText = "truncate table dbo.Pages";
            cmd.ExecuteNonQuery();

            cmd.CommandText = @"insert into dbo.Pages(PID, PageName, SubTitle, URL, Icon, IsShow, Descr, Sort) 
                                values(@PID, @PageName, @SubTitle, @URL, @Icon, @IsShow, @Descr, @Sort); select SCOPE_IDENTITY();";
            cmd.Parameters.Add("@PID", SqlDbType.Int);
            cmd.Parameters.Add("@PageName", SqlDbType.NVarChar, 10);
            cmd.Parameters.Add("@SubTitle", SqlDbType.NVarChar, 50);
            cmd.Parameters.Add("@URL", SqlDbType.VarChar, 100);
            cmd.Parameters.Add("@Icon", SqlDbType.VarChar, 20);
            cmd.Parameters.Add("@IsShow", SqlDbType.Bit);
            cmd.Parameters.Add("@Descr", SqlDbType.NVarChar, 100);
            cmd.Parameters.Add("@Sort", SqlDbType.Int);

            foreach (TBM.Models.MainMenu item in menu.Pages)
            {
                item.PID = 0;
                createMenuItem(item, cmd);
            }

            cmd.Connection.Close();
            MessageBox.Show("完成");
        }

        private void createMenuItem(TBM.Models.MainMenu item, SqlCommand cmd)
        {
            if (item == null) return;

            cmd.Parameters["@PID"].Value = item.PID;
            cmd.Parameters["@PageName"].Value = TBM.Core.CommandParamType.NullOrString(item.PageName);
            cmd.Parameters["@SubTitle"].Value = TBM.Core.CommandParamType.NullOrString(item.SubTitle);
            cmd.Parameters["@URL"].Value = item.URL;
            cmd.Parameters["@Icon"].Value = TBM.Core.CommandParamType.NullOrString(item.Icon);
            cmd.Parameters["@IsShow"].Value = item.IsShow;
            cmd.Parameters["@Descr"].Value = TBM.Core.CommandParamType.NullOrString(item.Descr);
            cmd.Parameters["@Sort"].Value = item.Sort;
            item.ID = Convert.ToInt32(cmd.ExecuteScalar());

            if (item.SubMenu == null) return;

            foreach (TBM.Models.MainMenu sitem in item.SubMenu)
            {
                sitem.PID = item.ID;
                createMenuItem(sitem, cmd);
            }
        }
    }
}
